package com.roncoo.education.api;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.NewsCategoryRequest;
import com.roncoo.education.api.request.NewsRequest;
import com.roncoo.education.db.entity.News;
import com.roncoo.education.db.entity.NewsCategory;
import com.roncoo.education.db.entity.NewsComment;
import com.roncoo.education.db.service.NewsCategoryService;
import com.roncoo.education.db.service.NewsCommentService;
import com.roncoo.education.db.service.NewsService;
import com.roncoo.education.util.MyJwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class NewsAdminController {

    @Resource
    private NewsService newsService;

    @Resource
    private NewsCategoryService newsCategoryService;

    @Resource
    private NewsCommentService newsCommentService;
    @Resource
    MyJwtUtil myJwtUtil;

    @PostMapping("/saveNewsCategory")
    public String saveNewsCategory(@Valid @RequestBody NewsCategoryRequest req, HttpServletRequest request){
        //String token = request.getHeader("Authorization");
        //Long adminId = myJwtUtil.getUsernameFromToken(token);
        Map<String,String> ret = newsCategoryService.saveNewsCategory(req);
        return JSONUtil.toJsonStr(ret);
    }

    @GetMapping("/getNewsCategorys")
    public String getNewsCategorys(Integer isHot){
        NewsCategory query =new NewsCategory();
        query.setIsHot(isHot);
        query.setDelFlag(1);
        query.setOrderClause(" sn asc");
        List<NewsCategory> list = newsCategoryService.findByBean(query);
        return JSONUtil.toJsonStr(list);
    }

    @PostMapping("/saveNews")
    public String saveNews(@Valid @RequestBody NewsRequest req, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long adminId = myJwtUtil.getUsernameFromToken(token);
        Map<String,String> ret = newsService.saveNews(adminId,req);
        return JSONUtil.toJsonStr(ret);
    }

    @PostMapping("/setDeleteNews")
    public String setDeleteNews(Long id){
        newsService.setDeleteNews(id);
        return "ok";
    }

    @PostMapping("/getNewsPage")
    public String getNewsPage(@Valid @RequestBody NewsRequest req){
        PageInfo<News> pageInfo = newsService.getNewsPages(req);
        return JSONUtil.toJsonStr(pageInfo);
    }

    @GetMapping("/getNews")
    public String getNewsDetail(Long id){
        News news = newsService.getNewById(id);
        return JSONUtil.toJsonStr(news);
    }

    @PostMapping("/getNewsCommentPage")
    public String getNewsCommentPages(@Valid @RequestBody NewsRequest req){
        //模糊匹配标题和分类查询评论列表
        PageInfo<NewsComment> pageInfo = newsCommentService.getCommentPages(req);
        return JSONUtil.toJsonStr(pageInfo);
    }

    @PostMapping("/setDelNewsComment")
    public String setDelNewsComment(Long id){
        //逻辑删除新闻评论
        newsCommentService.setDelFlag(id);
        return "ok";
    }
}
